from django.shortcuts import render
from ..renew.models import Tv, Movie, Cartoon
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage


def home(request):
    # 到数据库查询数据
    tv_lists = Tv.objects.all().order_by('-id')[0:8]
    movie_lists = Movie.objects.all().order_by('-id')[0:8]
    cartoon_lists = Cartoon.objects.all().order_by('-id')[0:8]
    # 组织数据
    context = {
        "tv_lists": tv_lists,
        "movie_lists": movie_lists,
        "cartoon_lists": cartoon_lists,
    }
    # 传递数据给模板
    return render(request, "home.html", context=context)


def tv(request):
    tv_lists = Tv.objects.all().order_by('-id')  # 到数据库查询数据
    paginator = Paginator(tv_lists, 40)  # 每一页多少条数据
    page = request.GET.get('page')  # 获取第几页
    try:
        contacts = paginator.page(page)
        tv_lists = contacts.object_list
    except PageNotAnInteger:
        contacts = paginator.page(1)
        tv_lists = contacts.object_list
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
        tv_lists = contacts.object_list
    return render(request, 'tv.html', {'contacts': contacts, 'tv_lists': tv_lists})


def movie(request):
    movie_lists = Movie.objects.all().order_by('-id')  # 到数据库查询数据
    paginator = Paginator(movie_lists, 40)  # 每一页多少条数据
    page = request.GET.get('page')  # 获取第几页
    try:
        contacts = paginator.page(page)
        movie_lists = contacts.object_list
    except PageNotAnInteger:
        contacts = paginator.page(1)
        movie_lists = contacts.object_list
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
        movie_lists = contacts.object_list
    return render(request, 'movie.html', {'contacts': contacts, 'movie_lists': movie_lists})


def cartoon(request):
    cartoon_lists = Cartoon.objects.all().order_by('-id')  # 到数据库查询数据
    paginator = Paginator(cartoon_lists, 40)  # 每一页多少条数据
    page = request.GET.get('page')  # 获取第几页
    try:
        contacts = paginator.page(page)
        cartoon_lists = contacts.object_list
    except PageNotAnInteger:
        contacts = paginator.page(1)
        cartoon_lists = contacts.object_list
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)
        cartoon_lists = contacts.object_list
    return render(request, 'cartoon.html', {'contacts': contacts, 'cartoon_lists': cartoon_lists})


def record(request):
    play_tv_title = request.COOKIES.get('play_tv_title', 'No record').encode('iso-8859-1').decode('utf-8')
    play_tv_href = request.COOKIES.get('play_tv_href', '')
    play_movie_title = request.COOKIES.get('play_movie_title', 'No record').encode('iso-8859-1').decode('utf-8')
    play_movie_href = request.COOKIES.get('play_movie_href', '')
    play_cartoon_title = request.COOKIES.get('play_cartoon_title', 'No record').encode('iso-8859-1').decode('utf-8')
    play_cartoon_href = request.COOKIES.get('play_cartoon_href', '')
    return render(request, 'record.html', {
        'play_tv_title': play_tv_title, 'play_tv_href': play_tv_href,
        'play_movie_title': play_movie_title, 'play_movie_href': play_movie_href,
        'play_cartoon_title': play_cartoon_title, 'play_cartoon_href': play_cartoon_href,
    })
